﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

namespace BillingManagment
{
    public partial class Billing : System.Web.UI.Page
    {
        protected DataTable dt = new DataTable();
        SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["connection"].ToString());
        SqlDataAdapter da;
        int requestID = 0;
        DbConnect db;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(Request.QueryString["RequestID"]) && !string.IsNullOrEmpty(Request.QueryString["Farmer_Id"]))
            {
                int requestID = Convert.ToInt32(Request.QueryString["RequestID"]);
                BindData();
                BindOtherData(Request.QueryString["Farmer_Id"].ToString(), Request.QueryString["RequestID"].ToString());
            }
            else
            {
                Response.Redirect("AllSellingSlip.aspx");
            }
        }

        private void BindOtherData(string Farmer_Id, string RequestID)
        {
            db = new DbConnect();
            DataSet ds = new DataSet();
            ds = db.GetDataSet("SP_Get_tbl_Inventory_History", CommandType.StoredProcedure,
                new SqlParameter("@Farmer_Id", Farmer_Id),
                new SqlParameter("@RequestID", RequestID));

            if (ds.Tables[0].Rows.Count > 0)
            {
                txtFarmer.Text = ds.Tables[0].Rows[0]["Name"].ToString();
                txtPrevious.Text = ds.Tables[0].Rows[0]["PreviousBucket"].ToString();
                txtSold.Text = ds.Tables[0].Rows[0]["Sold_Bucket"].ToString();
                txtReturn.Text = ds.Tables[0].Rows[0]["Return_Bucket"].ToString();
                txtBucket.Text = ds.Tables[0].Rows[0]["Total_Bucket"].ToString();
                txtRemaining.Text = ds.Tables[0].Rows[0]["Received_Bucket"].ToString();
                txtDate.Text=(Convert.ToDateTime(Request.QueryString["S_Date"].ToString())).ToString("dd/MM/yyyy");
            }

            db = new DbConnect();
            DataSet ds1 = new DataSet();
            ds1 = db.GetDataSet("SP_Get_tbl_Current_Ac", CommandType.StoredProcedure,
                new SqlParameter("@Farmer_Id", Farmer_Id),
                new SqlParameter("@Date", Convert.ToDateTime(Request.QueryString["S_Date"].ToString())));

            if (ds1.Tables[0].Rows.Count > 0)
            {
                txtProductAmount.Text = ds1.Tables[0].Rows[0]["ProductAmount"].ToString();
                txtLabourCharge.Text = ds1.Tables[0].Rows[0]["LabourCharge"].ToString();
                txtTotal.Text = ds1.Tables[0].Rows[0]["Total"].ToString();
                txtOpeningBalance.Text = ds1.Tables[0].Rows[0]["PreviousAmount"].ToString();
                txtAmountPaid.Text = ds1.Tables[0].Rows[0]["PayedAmount"].ToString();
                txtBalanceAmount.Text = ds1.Tables[0].Rows[0]["Balance"].ToString();
            }

            db = new DbConnect();
            DataSet ds2 = new DataSet();
            ds2 = db.GetDataSet("SP_Get_tbl_Current_Ac", CommandType.StoredProcedure,
                new SqlParameter("@Farmer_Id", Farmer_Id),
                new SqlParameter("@Date", Convert.ToDateTime(Request.QueryString["S_Date"].ToString())));

            if (ds2.Tables[0].Rows.Count > 0)
            {
                lblAdvance.Text = ds2.Tables[0].Rows[0]["Advance"].ToString();
                lblCash.Text = ds2.Tables[0].Rows[0]["Cash"].ToString();
                lblFraid.Text = ds2.Tables[0].Rows[0]["Fraid"].ToString();
                lblHydro.Text = ds2.Tables[0].Rows[0]["Hydro"].ToString();
                lblOther.Text = ds2.Tables[0].Rows[0]["Other"].ToString();
            }
        }


        public void BindData()
        {
            db = new DbConnect();
            DataSet dtItem = new DataSet();
            String qurr = "select * from tbl_Selling_Items where S_ID='" + Convert.ToInt32(Request.QueryString["RequestID"].ToString()) + "'";
            dtItem = db.GetDataSet(qurr, CommandType.Text);
            GridView1.DataSource = dtItem.Tables[0];
            GridView1.DataBind();
        }        
    }
}